const API = require("../../../../utils/api")
const app = getApp()
const util = require('../../../../utils/util')

// pages/mine/share/member-info/member-info.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum: 1,
        records:[],
        reachBottom: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userId: options.id
        })
        this.get_custom_info()
        this.get_user_records()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if( this.data.reachBottom ){
            let pageNum = this.data.pageNum
            pageNum++
            this.setData({
                pageNum: pageNum
            })
            this.get_user_records()
        }
    },

    /**
     * 获取直推详情
     */
    get_custom_info:function(){
        let that = this
        API.get_custom_info({
            userId: that.data.userId
        }).then( res => {
            if( res.code == 200 ){
                let info = res.data
                info.joinTeamTime = util.formatTime( new Date(info.joinTeamTime) )
                info.lastLoginTime = util.formatTime( new Date(info.lastLoginTime) )
                info.registerTime = util.formatTime( new Date(info.registerTime) )
                that.setData({
                    info: info
                })
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
        })
    },

    /**
     * 获取日志
     */
    get_user_records:function(){
        let that = this
        API.get_user_records({
            userId: that.data.userId,
            pageSize: 10,
            pageNum: that.data.pageNum
        }).then( res => {
            if( res.code == 200 ){
                that.setData({
                    records: [...that.data.records, ...res.rows]
                })
                if( res.total <= that.data.pageNum * 10 ){
                    that.setData({
                        reachBottom: false
                    })
                }
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
        })
    },

    /**
     * 联系 - 拨打电话
     */
    contact:function(){
        wx.makePhoneCall({
          phoneNumber: this.data.info.phone,
        })
    },
})